package com.ldcc.report_system.ui.user;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.ldcc.report_system.ReportApplication;
import com.ldcc.report_system.component.LogoutDialog;
import com.ldcc.report_system.databinding.DialogAlertBinding;
import com.ldcc.report_system.databinding.FragmentPersonalCenterBinding;
import com.ldcc.report_system.ui.LoginActivity;
import com.ldcc.report_system.ui.setting.AboutSoftwareActivity;
import com.ldcc.report_system.util.ActivitySkipUtil;
import com.ldcc.report_system.util.AlertUtil;
import com.ldcc.report_system.util.http.ClientRequestManage;
import com.ldcc.report_system.util.http.HttpObserver;
import com.ldcc.report_system.util.http.ServerResponse;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author wwg
 * @description 个人中心界面
 */
public class PersonalCenterFragment extends Fragment {

    public static final String TAG = "personalCenterFragment";
    private AppCompatActivity parentActivity;
    private FragmentPersonalCenterBinding personalCenterBinding;
    private DialogAlertBinding dialogBinding;
    private LogoutDialog logoutDialog;

    public PersonalCenterFragment(AppCompatActivity parentActivity) {
        this.parentActivity = parentActivity;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.personalCenterBinding = FragmentPersonalCenterBinding.inflate(inflater, container, false);
        this.dialogBinding = DialogAlertBinding.inflate(inflater, container,false);
        // 点击"我的信息"
        personalCenterBinding.personalCenterMyInfo.setOnClickListener(view -> {
                ActivitySkipUtil.skipAnotherActivity(getActivity(), MyInfoActivity.class);
            });
        // 点击“关于软件”
        personalCenterBinding.aboutSoftware.setOnClickListener(view -> {
            ActivitySkipUtil.skipAnotherActivity(getActivity(), AboutSoftwareActivity.class);
        });
        // 点击“通用”
//        personalCenterBinding.personalCenterCommon.setOnClickListener(view -> {
//            ActivitySkipUtil.skipAnotherActivity(getActivity(), CommonActivity.class);
//        });

        // 点击“退出登录”
        personalCenterBinding.personalCenterLogout.setOnClickListener(view -> {
            this.showAlertDialog();
        });
        return this.personalCenterBinding.getRoot();
    }

    /**
     * 退出登录事件
     */
    private void logout() {

//        AlertDialog.Builder builder = new AlertDialog.Builder(Objects.requireNonNull(getContext()));
//        builder.setView(this.dialogBinding.getRoot())
//                .create().show();
//        builder.setMessage("确定退出吗")
//                .setView(this.dialogBinding.getRoot())
//                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialogInterface, int i) {
//                        Log.e(TAG, "onClick: pressed positive button");
//                    }
//                })
//                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialogInterface, int i) {
//                        Log.e(TAG, "onClick: pressed negative button");
//                    }
//                })
//                .create()
//                .show();
        ClientRequestManage.getInstance().getUserApi().logout().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<Object>(ReportApplication.getContext(), null) {

                    @Override
                    public void onSuccess(ServerResponse<Object> response_entity) {
                        AlertUtil.shortTime("已登出");
                        ActivitySkipUtil.skipAnotherActivity(getActivity(), LoginActivity.class);
                    }
                });
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 初始化
        this.init();
    }

    private void init() {

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    /**
     * 展示确认登录的弹框
     */
    private void showAlertDialog() {
       logoutDialog = LogoutDialog.with(parentActivity)
                .setCancelable(true)
                .setContent("确定退出登录吗？")
                .setPositiveButton("确定", view -> logout())
               .setNegativeButton("取消", view -> logoutDialog.dismiss())
               .show();
    }
}